<?php
include_once("../../untils/conn.php");

if (empty($_GET['id'])) {
    exit('<h1>连接数据库失败</h1>');
}

$id = $_GET['id'];
$dz_time = time();
$cr_time = time();
$note = "下级订单结算，平台返佣￥" . $proxy_prices;
$notes = "下级订单结算，平台返佣￥" . $proxy_pricess;

// 查询revenue表中的数据
$query = "SELECT * FROM revenue WHERE id = $id";
$result = mysqli_query($con, $query);

$row = mysqli_fetch_assoc($result);

// 提取字段值为变量
$account = $row['account'];
$orderid = $row['oderid'];
$name = $row['name'];
$price = $row['price'];
$proxy_upid = $row['proxy_upid'];
$proxy_prices = $row['proxy_prices'];
$proxy_upids = $row['proxy_upids'];
$proxy_pricess = $row['proxy_pricess'];

$note = "下级订单完成，平台返佣￥" . $proxy_prices;
$notes = "下级订单完成，平台返佣￥" . $proxy_pricess;

// 处理第一种情况：只有account和price有值
if (!empty($account) && !empty($price) && empty($proxy_upid) && empty($proxy_prices) && empty($proxy_upids) && empty($proxy_pricess)) {
    // 查询proxy表中proxy_acc = account的数据，并给字段proxy_balance的值加上price
    $updateQuery = "UPDATE proxy SET proxy_balance = proxy_balance + $price WHERE proxy_acc = '$account'";
    $updateResult = mysqli_query($con, $updateQuery);

    if (!$updateResult) {
        echo '<script>alert("结算失败");history.go(-1);</script>';
        exit;
    }

    // 更新revenue表中的数据
    $updateQuery = "UPDATE revenue SET state = 2, type = 0, dz_time = $dz_time WHERE id = $id";
    $updateResult = mysqli_query($con, $updateQuery);

    if (!$updateResult) {
        echo '<script>alert("结算失败");history.go(-1);</script>';
        exit;
    }

    echo '<script>alert("结算成功");history.go(-1);</script>';
}

// 处理第二种情况：account、price、proxy_upid和proxy_prices有值
if (!empty($account) && !empty($price) && !empty($proxy_upid) && !empty($proxy_prices) && empty($proxy_upids) && empty($proxy_pricess)) {
    // 使用第一种方式更新完成后，查询proxy表中proxy_acc = proxy_upid的数据，并给字段proxy_balance的值加上proxy_prices
    $updateQuery = "UPDATE proxy SET proxy_balance = proxy_balance + $price WHERE proxy_acc = '$account'";
    $updateResult = mysqli_query($con, $updateQuery);

    if (!$updateResult) {
        echo '<script>alert("结算失败");history.go(-1);</script>';
        exit;
    }

    // 更新revenue表中的数据
    $updateQuery = "UPDATE revenue SET state = 2, type = 0, dz_time = $dz_time WHERE id = $id";
    $updateResult = mysqli_query($con, $updateQuery);

    if (!$updateResult) {
        echo '<script>alert("结算失败");history.go(-1);</script>';
        exit;
    }

    $updateQuery = "UPDATE proxy SET proxy_balance = proxy_balance + $proxy_prices WHERE proxy_acc = '$proxy_upid'";
    $updateResult = mysqli_query($con, $updateQuery);

    if (!$updateResult) {
        echo '<script>alert("结算失败");history.go(-1);</script>';
        exit;
    }

    // 在revenue表中插入一条数据
    $insertQuery = "INSERT INTO revenue (account, oderid, name, price,oder_sta , state, type, dz_time, cr_time, beizhu) VALUES ('$proxy_upid', '$orderid', '$name', $proxy_prices,3, 2, 0, $dz_time, $cr_time, '$note')";
    $insertResult = mysqli_query($con, $insertQuery);

    if (!$insertResult) {
        // echo '<script>alert("结算失败");history.go(-1);</script>';
        echo "Error updating record: " . $con->error;
        exit;
    }

    echo '<script>alert("结算成功");history.go(-1);</script>';
}

// 处理第三种情况：account、price、proxy_upid、proxy_prices、proxy_upids和proxy_pricess都有值
if (!empty($account) && !empty($price) && !empty($proxy_upid) && !empty($proxy_prices) && !empty($proxy_upids) && !empty($proxy_pricess)) {
    // 使用第一种方式和第二种方式执行完后，查询proxy表中proxy_acc = proxy_upids的数据，并给字段proxy_balance的值加上proxy_pricess
    
     $updateQuery = "UPDATE proxy SET proxy_balance = proxy_balance + $price WHERE proxy_acc = '$account'";
    $updateResult = mysqli_query($con, $updateQuery);

    if (!$updateResult) {
        echo '<script>alert("结算失败");history.go(-1);</script>';
        exit;
    }

    // 更新revenue表中的数据
    $updateQuery = "UPDATE revenue SET state = 2, type = 0, dz_time = $dz_time WHERE id = $id";
    $updateResult = mysqli_query($con, $updateQuery);

    if (!$updateResult) {
        echo '<script>alert("结算失败");history.go(-1);</script>';
        exit;
    }

    $updateQuery = "UPDATE proxy SET proxy_balance = proxy_balance + $proxy_prices WHERE proxy_acc = '$proxy_upid'";
    $updateResult = mysqli_query($con, $updateQuery);

    if (!$updateResult) {
        echo '<script>alert("结算失败");history.go(-1);</script>';
        exit;
    }

   // 在revenue表中插入一条数据
    $insertQuery = "INSERT INTO revenue (account, oderid, name, price,oder_sta , state, type, dz_time, cr_time, beizhu) VALUES ('$proxy_upid', '$orderid', '$name', $proxy_prices,3, 2, 0, $dz_time, $cr_time, '$note')";
    $insertResult = mysqli_query($con, $insertQuery);

    if (!$insertResult) {
        // echo '<script>alert("结算失败");history.go(-1);</script>';
        echo "Error updating record: " . $con->error;
        exit;
    }

    
    $updateQuery = "UPDATE proxy SET proxy_balance = proxy_balance + $proxy_pricess WHERE proxy_acc = '$proxy_upids'";
    $updateResult = mysqli_query($con, $updateQuery);

    if (!$updateResult) {
        echo '<script>alert("结算失败");history.go(-1);</script>';
        exit;
    }

    // 在revenue表中插入一条数据
    $insertQuery = "INSERT INTO revenue (account, oderid, name, price, oder_sta, state, type, dz_time, cr_time, beizhu) VALUES ('$proxy_upids', '$orderid', '$name', $proxy_pricess,3, 2, 0, $dz_time, $cr_time, '$notes')";
    $insertResult = mysqli_query($con, $insertQuery);

    if (!$insertResult) {
        echo '<script>alert("结算失败");history.go(-1);</script>';
        exit;
    }

       echo '<script>alert("结算成功");history.go(-1);</script>';
}

mysqli_close($con);
?>
